<?php
// 更新设置选项
function settings_recache()	{
	global $db, $tablepre;
	$settings = $db->query("SELECT title, value FROM {$tablepre}xna_settings");
	$contents = "\$options = array(\r\n";
	while ($setting = $db->row($settings)) {
		$contents.="\t'".addslashes($setting['title'])."' => '".addslashes($setting['value'])."',\r\n";
	}
	$contents .= ');';
	writetocache('settings',$contents);
}

// 更新分类
function categories_recache() {
	global $db, $tablepre;
	$categories = $db->query("SELECT * FROM {$tablepre}xna_category ORDER BY cid");
	$contents = "\$catecache = array(\r\n";
	while ($cate = $db->row($categories)) {
		$contents.="\t'".$cate['cid']."' => array(\r\n\t\t'cid' => '".$cate['cid']."',\r\n\t\t'cate_title' =>'".addslashes($cate['cate_title'])."',\r\n\t\t'cate_content' => '".addslashes($cate['cate_content'])."',\r\n\t\t'feed_count' => '".$cate['feed_count']."',\r\n\t),\r\n";
	}
	$contents .= ');';
	writetocache('categories',$contents);
}

// 更新链接
function links_recache() {
	global $db, $tablepre;
	$links = $db->query("SELECT linkid,link_title,link_url,link_content FROM {$tablepre}xna_links  ORDER BY displayorder ASC, link_title ASC");
	$linkdb = array();
	while ($link = $db->row($links)) {
		$linkdb[] = $link;
	}
	unset($link);
	$tatol = count($linkdb);

	$contents  = "\$link_count = $tatol;\r\n";
	$contents .= "\$linkcache = array(\r\n";
	foreach($linkdb as $link){
		$contents.="\t'".$link['linkid']."' => array(\r\n\t\t'link_title' => '".addslashes($link['link_title'])."',\r\n\t\t'link_url' => '".addslashes($link['link_url'])."',\r\n\t\t'link_content' => '".addslashes($link['link_content'])."',\r\n\t),\r\n";
	}
	$contents .= ');';
	writetocache('links',$contents);

}

// 更新最新评论
function newcomments_recache() {
	global $db, $tablepre;
	$query = $db->query("SELECT * FROM {$tablepre}xna_settings");
	$set = array();
	while ($r = $db->row($query)) {
		$set[$r['title']] = $r['value'];
	}
	unset($r);
	$newcomments = $db->query("SELECT c.commid, c.author, c.addtime, c.content FROM {$tablepre}xna_comment c LEFT JOIN {$tablepre}xna_feed a ON (a.feedid=c.feedid) WHERE a.state='1' AND c.state='1' ORDER BY commid DESC LIMIT ".intval($set['recentcomment_num']));
	$contents = "\$newcommentcache = array(\r\n";
	$i=0;
	while ($newcomment = $db->row($newcomments)) {
		$newcomment['content'] = preg_replace("/\[quote=(.*?)\]\s*(.+?)\s*\[\/quote\]/is", "", $newcomment['content']);
		if (empty($newcomment['content'])) {
			$newcomment['content'] = '......';
		}
		$contents.="\t'".$i."' => array(\r\n\t\t'commentid' => '".$newcomment['commid']."',\r\n\t\t'feedid' => '".$newcomment['feedid']."',\r\n\t\t'author' => '".addslashes($newcomment['author'])."',\r\n\t\t'addtime' => '".sadate($set['recent_comment_timeformat'], $newcomment['addtime'])."',\r\n\t\t'content' => '".htmlspecialchars(addslashes(str_replace(array("\r\n","\n","\r"),'',$newcomment['content'])), $set['recentcomment_limit'])."',\r\n\t),\r\n";
		unset($newcomment);
		$i++;
	}
	$contents .= ');';
	writetocache('newcomments',$contents);
}

// 更新统计
function statistics_recache() {
	global $db, $tablepre, $timestamp;
	$statistics = $db->query("SELECT statid,site_count,cate_count,feed_count,user_count,comment_count,tag_count,today_view_count,all_view_count FROM {$tablepre}xna_statistics");
	//设置存活变量
	$contents  = "\$stats_expire = '".($timestamp + 600)."';\r\n";
	$contents .= "\$stats = array(\r\n";
	while ($stat = $db->row($statistics)) {
		$contents .= "\t'site_count' => '".$stat['site_count']."',\r\n\t'cate_count' => '".$stat['cate_count']."',\r\n\t'feed_count' => '".$stat['feed_count']."',\r\n\t'comment_count' => '".$stat['comment_count']."',\r\n\t'tag_count' => '".$stat['tag_count']."',\r\n\t'user_count' => '".$stat['user_count']."',\r\n\t'today_view_count' => '".$stat['today_view_count']."',\r\n\t'all_view_count' => '".$stat['all_view_count']."',\r\n";
	}
	$contents .= ');';
	writetocache('statistics',$contents);
}

// 更新站点统计
function sites_recache() {
	global $db, $tablepre, $timestamp;
	$sites = $db->query("SELECT siteid,site_title,site_url,site_count,feed_count FROM {$tablepre}xna_site WHERE state = '1'");
	//设置存活变量
	$contents  = "\$sites_expire = '".($timestamp + 600)."';\r\n";
	$contents .= "\$sites = array(\r\n";
	while ($site = $db->row($sites)) {
		$contents .="\t'".$site['siteid']."' => array(\r\n\t\t'site_title' => '".$site['site_title']."',\r\n\t\t'site_url' => '".$site['site_url']."',\r\n\t\t'site_count' => '".$site['site_count']."',\r\n\t\t'feed_count' => '".$site['feed_count']."',\r\n\t\t),\r\n";
			}
			$contents .= ');';
	writetocache('sites',$contents);
}

// 写入缓存文件
function writetocache($cachename, $cachedata = '') {
	if(in_array($cachename, array('categories','sites','links','comments','settings','statistics','stylevars','autosave'))) {
		$cachedir = _ROOT.'cache/';
		$cachefile = $cachedir.'cache_'.$cachename.'.php';
		if(!is_dir($cachedir)) {
			@mkdir($cachedir, 0777);
		}
		if($fp = @fopen($cachefile, 'wb')){
			@fwrite($fp, "<?php\r\n//Created on ".sadate('Y-m-d H:i:s')."\r\n\r\nif(!defined('IN_ROOT')) exit('Access Denied');\r\n\r\n".$cachedata."\r\n\r\n?>");
			@fclose($fp);
			@chmod($cachefile, 0777);
		} else {
			echo 'Can not write to '.$cachename.' cache files, please check directory ./cache/ .';
			exit;
		}
	}
}

// 重新统计各种数据
function rethestats($cachename = '') {
	global $db, $tablepre;
	if (!$cachename || $cachename == 'settings') {
		settings_recache();
	}
	if (!$cachename || $cachename == 'statistics') {
		statistics_recache();
	}
	if (!$cachename || $cachename == 'categories') {
		categories_recache();
	}
	if (!$cachename || $cachename == 'links') {
		links_recache();
	}
	if (!$cachename || $cachename == 'newcomments') {
		newcomments_recache();
	}	
	if (!$cachename || $cachename == 'sites') {
		sites_recache();
	}	
}
?>